package com.it.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.it.domain.SalesOrder;

import org.apache.ibatis.annotations.*;
import org.mapstruct.Mapper;


import java.util.List;

@Mapper
public interface SalesOrderMapper extends BaseMapper<SalesOrder> {
    //获取所有销售信息
    @Select("SELECT * FROM sales_order")
    List<SalesOrder> selectAll();
    //获取单个销售信息
    @Select("SELECT * FROM sales_order WHERE sales_order_id = #{id}")
    SalesOrder selectById(Integer id);
    //创建销售订单
    @Insert("INSERT INTO sales_order (customer_id, order_date, status, total_amount, create_by, update_by) " +
            "VALUES (#{customerId}, #{orderDate}, #{status}, #{totalAmount}, #{createBy}, #{updateBy})")
    @Options(useGeneratedKeys = true, keyProperty = "salesOrderId")
    int insert(SalesOrder salesOrder);
    //更新销售订单
    @Update("UPDATE sales_order SET customer_id=#{customerId}, order_date=#{orderDate}, " +
            "status=#{status}, total_amount=#{totalAmount}, update_by=#{updateBy} " +
            "WHERE sales_order_id=#{salesOrderId}")
    int update(SalesOrder salesOrder);
    //删除销售订单
    @Delete("DELETE FROM sales_order WHERE sales_order_id=#{id}")
    int delete(Integer id);

    // 关联查询销售订单和客户信息
    //1.获取所有销售订单（带客户信息）
    @Select("SELECT so.*, c.* FROM sales_order so LEFT JOIN customer c ON so.customer_id = c.customer_id")
    @ResultMap("salesOrderResultMap")
    List<SalesOrder> selectAllWithCustomer();
    //2.获取单个销售订单（带客户信息）
    @Select("SELECT so.*, c.* FROM sales_order so LEFT JOIN customer c ON so.customer_id = c.customer_id " +
            "WHERE so.sales_order_id = #{id}")
    @ResultMap("salesOrderResultMap")
    SalesOrder selectByIdWithCustomer(Integer id);
}
